package com.liao.badminton.code;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;

public class ResponseRest<T> {
    private String ope = "default";
    private Boolean suc = false;
    private Integer code = RespCodeMessage.SYSTEM_ERROR.getCode();
    private String msg = RespCodeMessage.SYSTEM_ERROR.getMessage();
    private T data;

    public String getOpe() {
        return ope;
    }

    public Boolean getSuc() {
        return suc;
    }

    public T getData() {
        return data;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public ResponseRest() {
    }

    public ResponseRest setOpe(String operate) {
        this.ope = operate;
        return this;
    }

    public ResponseRest setCode(int code) {
        this.code = code;
        return this;
    }

    public ResponseRest setMsg(String message) {
        if (this.msg != null && this.msg.contains("：{}")) {
            if (message != null)
                this.msg = StrUtil.format(getMsg(), message);
            else
                this.msg = msg.replace("：{}", "");
        } else if (message != null) {
            this.msg = message;
        }
        return this;
    }

    public ResponseRest setSuc(Boolean suc) {
        this.suc = suc;
        return this;
    }

    public ResponseRest setData(T data) {
        this.data = data;
        return this;
    }

    public ResponseRest(String ope) {
        this.ope = ope;
    }

    /////////////////////// 各种简便的静态工厂方法 ///////////////////////
    ////// 操作成功的：

    public static ResponseRest suc(String operate) {
        return new ResponseRest(operate).setRespCodeMessage(RespCodeMessage.SYSTEM_SUCCESS).setSuc(true);
    }

    public static ResponseRest suc(String operate, Object data) {
        return suc(operate).setData(data);
    }

    ////// 操作失败的：
    public static ResponseRest fail(String operate) {
        return new ResponseRest(operate);
    }

    public static ResponseRest fail(String operate, String message) {
        return new ResponseRest(operate).setMsg(message);
    }

    public static ResponseRest fail(String operate, RespCodeMessage codeMessage) {
        return new ResponseRest(operate).setRespCodeMessage(codeMessage);
    }

    public static ResponseRest fail(String operate, RespCodeMessage codeMessage, String selfMsg) {

        return fail(operate, codeMessage).setMsg(selfMsg);
    }

    public static ResponseRest fail(RespCodeMessage codeMessage, String selfMsg) {

        return new ResponseRest().setRespCodeMessage(codeMessage).setMsg(selfMsg);
    }

    public ResponseRest setRespCodeMessage(RespCodeMessage respCodeMessage) {
        return setCodeAndMsg(respCodeMessage.getCode(), respCodeMessage.getMessage());
    }

    public ResponseRest setCodeAndMsg(int code, String message) {
        this.code = code;
        this.msg = message;
        return this;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
